part of '../widgets.dart';

enum VanSpaceDirection { horizontal, vertical }

enum VanSpaceAlign {
  start(MainAxisAlignment.start, CrossAxisAlignment.start),
  end(MainAxisAlignment.end, CrossAxisAlignment.end),
  center(MainAxisAlignment.center, CrossAxisAlignment.center),
  baseline(MainAxisAlignment.center, CrossAxisAlignment.baseline);
  final MainAxisAlignment _mainAxisAlignment;
  final CrossAxisAlignment _crossAxisAlignment;

  const VanSpaceAlign(this._mainAxisAlignment,this._crossAxisAlignment);
}

/// [Widget] 设置元素之间的间距
class VanSpace extends StatelessWidget {
  /// props - 间距方向(默认: horizontal)
  final VanSpaceDirection direction;

  /// props - 间距大小，如 20px 2em，默认单位为 px (默认: 8px)
  final double? size;

  /// props - 设置子元素的对齐方式(start | end | center | baseline)
  final VanSpaceAlign align;

  /// props - 是否自动换行，仅适用于水平方向排列(默认: false)
  final bool wrap;

  /// props - 是否让 Space 变为一个块级元素，填充整个父元素(默认: false)
  final bool fill;

  ///
  final VanSpaceStyle? style;

  /// slots - 间距组件内容
  final List<Widget> children;

  const VanSpace({
    super.key,
    this.direction = VanSpaceDirection.horizontal,
    this.size,
    this.align = VanSpaceAlign.start,
    this.wrap = false,
    this.fill = false,
    this.style,
    this.children = const [],
  });

  VanSpaceStyle getComputedStyle() {
    EdgeInsetsGeometry? padding = style?.padding ?? const EdgeInsets.all(8);

    if (!wrap && size != null) padding = EdgeInsets.all(size!);

    return VanSpaceStyle(padding: padding);
  }

  @override
  Widget build(BuildContext context) {
    final style = getComputedStyle();

    if (direction == VanSpaceDirection.vertical) {
      List<Widget>? widgets;
      if (children.isNotEmpty) {
        widgets = [];
        for (var i = 0; i < children.length; i++) {
          if (i == 0) {
            widgets.add(children[i]);
          } else {
            widgets.add(Padding(
              padding: EdgeInsets.only(top: size ?? 8),
              child: children[i],
            ));
          }
        }
      }
      Widget widget = Column(
        textBaseline: TextBaseline.alphabetic,
        crossAxisAlignment: align._crossAxisAlignment,
        mainAxisAlignment: align._mainAxisAlignment,
        children: widgets ?? children,
      );
      if (fill) widget = SizedBox(width: double.infinity, child: widget);
      return widget;
    }

    if (!wrap) {
      return LayoutBuilder(builder: (context, constraints) {
        List<Widget> ws = [];
        for (var i = 0; i < children.length; i++) {
          ws.add(Container(
            margin: i > 0 ? EdgeInsets.only(left: size ?? 8) : null,
            constraints: constraints,
            child: children[i],
          ));
        }


        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: Row(
            mainAxisSize: MainAxisSize.min,
            textBaseline: TextBaseline.alphabetic,
            crossAxisAlignment: align._crossAxisAlignment,
            mainAxisAlignment: align._mainAxisAlignment,
            children: ws,
          ),
        );
      });
    }
    return Wrap(runSpacing: size ?? 8, spacing: size ?? 8, children: children);
  }
}
